public class LeadExtension_Cls {

    public List<LeadWrapper> NewLeads{get;set;}
    public Boolean leadRowLimit{get;set;}
    public Boolean invalidData{get;set;}
    public string notes{get;set;}
    public Boolean checked{get;set;}
    
        
    
    Boolean invalidDisposition=false;

    public static integer ROWLIMIT = 10;
    
    public LeadExtension_Cls(ApexPages.StandardController myController) {
        NewLeads = new List<LeadWrapper>();
        NewLeads.add(new LeadWrapper(new Lead(), false));
        leadRowLimit = (NewLeads.size()<ROWLIMIT);
        checked=true;
    }


    public void addrow() {
        if (validateAddRow())
        {
            System.debug('****addrow() ***insideif****');
            return;
        }
        System.debug('****addrow() *******');
        checked=true;
        invalidData = false;    
        leadRowLimit = (NewLeads.size()<ROWLIMIT);
        System.debug('Add Row Flag : '+leadRowLimit);
        if(leadRowLimit){
            Lead leadObj = new Lead();
            NewLeads.add(new LeadWrapper(leadObj, false));
        }
    }
    
    private Boolean validateRemoveRow(){
       checked = false; 
       for(integer i=0 ; i< NewLeads.size(); i++){
              //System.assert(false,'Setting invalidDATA to true'+ NewLeads[i].leadObj.LastName);
              System.debug('****validateRemoveRow() ********inside for loop****');
              if(NewLeads[i].checked){
                      checked=true;
                      if(NewLeads[i].leadObj.LastName == null)
                          NewLeads[i].leadObj.LastName.addError('Disposition is needed when the notes are present');
                      if(NewLeads[i].leadObj.FirstName == null)
                          NewLeads[i].leadObj.FirstName.addError('Disposition is needed when the notes are present');
                      if(NewLeads[i].leadObj.Phone == null)
                          NewLeads[i].leadObj.Phone.addError('Disposition is needed when the notes are present');
                      if(NewLeads[i].leadObj.Company == null)
                          NewLeads[i].leadObj.Company.addError('Disposition is needed when the notes are present');
                      return true;
              }
               // System.debug(' NewLeads[i].leadObj.Disposition__c : '+NewLeads[i].leadObj.Disposition__c);
        }
        return false; 
    }  
    private Boolean validateAddRow(){

       for(integer i=0 ; i< NewLeads.size(); i++){
              //System.assert(false,'Setting invalidDATA to true'+ NewLeads[i].leadObj.LastName);  
              if((NewLeads[i].leadObj.LastName == null || NewLeads[i].leadObj.FirstName == null || NewLeads[i].leadObj.Phone == null || NewLeads[i].leadObj.Company == null)){
                      if(NewLeads[i].leadObj.LastName == null)
                          NewLeads[i].leadObj.LastName.addError('You must enter a value');
                      if(NewLeads[i].leadObj.FirstName == null)
                          NewLeads[i].leadObj.FirstName.addError('You must enter a value');
                      if(NewLeads[i].leadObj.Phone == null)
                          NewLeads[i].leadObj.Phone.addError('You must enter a value');
                      if(NewLeads[i].leadObj.Company == null)
                          NewLeads[i].leadObj.Company.addError('You must enter a value');
                      /*if((NewLeads[i].leadObj.Disposition__c == null)&&(NewLeads[i].leadObj.Most_Recent_Note__c != null))
                          NewLeads[i].leadObj.Disposition__c.addError('Disposition is Mandatory when the notes are present');*/
                      return true;
              }
               // System.debug(' NewLeads[i].leadObj.Disposition__c : '+NewLeads[i].leadObj.Disposition__c);
        }
        return false; 
    }       
    public void removerow(){
        if (!validateRemoveRow()) return;
        invalidData = false;
        Integer limit0 = NewLeads.size();
        
        integer i=0 ; 
        while(i<limit0){
        System.debug('XXXXXXXXXXXXXXXXXXXXXXXX');
        System.debug('NewLeads.size()  :'+NewLeads.size());
        System.debug('NewLeads[i] :'+NewLeads[i]);
        //System.assert(false, 'NewLeads[i] :'+NewLeads[i]);
            if(NewLeads[i].checked){
                 NewLeads.remove(i);
                 limit0= NewLeads.size();
                 if(i!=0){
                 i--;
                 }else i=0;
                 continue;
            }
            i++;
            System.debug('LIMIT : '+limit0+ '     i : '+i);
        }
        leadRowLimit = (NewLeads.size()<ROWLIMIT);
        }
            
    public PageReference save() {
        
    if (validateAddRow()) return null;
        invalidDisposition=false;
        invalidData = false;
         List<lead> leads = new List<Lead>();
        try{
            for(integer i=0 ; i< NewLeads.size(); i++){
                    NewLeads[i].leadObj.Multi_Lead__c=True;
                    leads.add(NewLeads[i].leadObj);
                    if(NewLeads[i].leadObj.LastName == '<value>' || NewLeads[i].leadObj.FirstName == '<value>' || NewLeads[i].leadObj.Phone == '<value>' || NewLeads[i].leadObj.Company == '<value>'){
                        System.debug('Setting invalidDATA to true');
                        invalidData = true;
                    }
               // System.debug(' NewLeads[i].leadObj.Disposition__c : '+NewLeads[i].leadObj.Disposition__c);

/*
                 if(NewLeads[i].leadObj.Notes__c!='' && NewLeads[i].leadObj.Disposition__c==null || NewLeads[i].leadObj.Disposition__c=='--None--')
                   {
                      invalidDisposition= true;
                      System.debug('Setting invalidDisposition to true, NewLeads[i].leadObj.Notes__c');
                      System.debug('**** save config');
                      NewLeads[i].leadObj.Disposition__c.addError('Disposition is needed when the notes are present');
                   }  
*/            
            }
            if(!invalidData){
                if(leads.size()>0){
                
                    insert leads;
                 
                
                }
                NewLeads.clear();
                leadRowLimit = true;
                addRow(); 
            }
        }
        catch(DmlException ex){
        ApexPages.addMessages(ex);
    }
    
        return null;        
    }
        
    public PageReference saveAndExit() {
    
    if (validateAddRow()) return null;
        List<lead> leads = new List<Lead>();
        try{
        for(integer i=0 ; i< NewLeads.size(); i++){
                NewLeads[i].leadObj.Multi_Lead__c=True;
                leads.add(NewLeads[i].leadObj);
                if(NewLeads[i].leadObj.LastName == '<value>' || NewLeads[i].leadObj.FirstName == '<value>' || NewLeads[i].leadObj.Phone == '<value>' || NewLeads[i].leadObj.Company == '<value>'){
                    invalidData = true;
                }
                
              //  System.debug(' NewLeads[i].leadObj.Disposition__c : '+NewLeads[i].leadObj.Disposition__c);
             /*
             // incorporated this in validation rule
              if(NewLeads[i].leadObj.Notes__c!='' && (NewLeads[i].leadObj.Disposition__c!=''))
               {
              invalidDisposition = true;
              NewLeads[i].leadObj.Disposition__c.addError('Disposition is needed when the notes are present');
              System.debug('**** save and exit function config');
              }  
              else invalidDisposition =false;
      */          
        }
      //if(!invalidData){
            if(leads.size()>0){
                insert leads;
            }
            
            PageReference home = new PageReference('/00Q?fcf=00B3000000779Zf');
            home.setRedirect(true);
            return home; 
       // }
        }
        catch(DmlException ex){
        ApexPages.addMessages(ex);
    }
        return null;
    }        
 
     public PageReference Cancel() {
        PageReference home = new PageReference('/00Q?fcf=00B3000000779Zf');
        home.setRedirect(true);
        return home; 
        
    }        
 
 class LeadWrapper {
 
     public Lead leadObj {get; set;}
     public Boolean checked {get; set;}
     public LeadWrapper (Lead leadObj0, Boolean checked0){
         leadObj = leadObj0;
         /*leadObj.company = '<value>';
         leadObj.FirstName = '<value>';
         leadObj.LastName = '<value>';
         leadObj.Phone = '<value>';
*/
         checked = checked0;
     }
 }
 
//test method  
static testMethod void testLeadExtension_Cls()
{

ID localLeadRecID = RecordTypeHelper.GetRecordTypeID('Lead', 'Local Advertiser'); 
Lead ld = new Lead(RecordTypeId=localLeadRecID,Company ='Test Demo50',FirstName='John',LastName='Smith',Phone='8989898950',Disposition__c='Ready for Order');
insert ld;

ApexPages.StandardController controller = new ApexPages.StandardController(ld);

LeadExtension_Cls newobj = new LeadExtension_Cls(controller);
newobj.NewLeads[0].checked = true;
newobj.addrow();
newobj.removerow();
newobj.save();
newobj.saveAndExit();
newobj.Cancel();
newobj.NewLeads.clear();
newobj.addrow();
newobj.NewLeads.clear();
newobj.saveAndExit();

}


 }